import 'package:boss/http/api/api_helper.dart' as apiHelper;
import 'package:boss/main.dart';
import 'package:boss/models/mch_data.dart';
import 'package:boss/models/pay_info_form.dart';
import 'package:boss/ui/page/binding/model/bankAccountType.dart';
import 'package:boss/ui/page/binding/page/bindingReview/binding_review_page.dart';
import 'package:boss/ui/widget/loading/loading.dart';
import 'package:city_pickers/city_pickers.dart' as cityPicker;
import 'package:flustars/flustars.dart' as flustars;
import 'package:flutter/material.dart';
import 'package:hi_common/hi_common.dart';

class BindingProvider extends ChangeNotifier {
  PayInfoForm payInfoForm;

  WechatHistory wechatHistory;

  WechatHistory alipayHistory;

  DateTime get idCardStartTime {
    if (payInfoForm == null || payInfoForm.idCardStartTime == null) {
      return null;
    }

    return flustars.DateUtil.getDateTime(payInfoForm.idCardStartTime);
  }

  DateTime get idCardEndTime {
    if (payInfoForm == null || payInfoForm.idCardEndTime == null) {
      return null;
    }

    return flustars.DateUtil.getDateTime(payInfoForm.idCardEndTime);
  }

  String get pcaName {
    var pca = '';
    if (payInfoForm.bankProvince != null) {
      pca = payInfoForm.bankProvince;
    }
    if (payInfoForm.bankCity != null) {
      pca += '-' + payInfoForm.bankCity;
    }
    if (payInfoForm.bankDistrict != null) {
      pca += '-' + payInfoForm.bankDistrict;
    }
    return pca;
  }

  BindingProvider({PayInfoForm payInfoForm}) {
    this.payInfoForm = payInfoForm ?? PayInfoForm.fromJson({});
  }

  setBankCardPositive(String value) {
    payInfoForm.bankCardPositive = value;
    notifyListeners();
  }

  setBankCardBack(String value) {
    payInfoForm.bankCardBack = value;
    notifyListeners();
  }

  setBankName(String value) {
    payInfoForm.bankName = value;
    notifyListeners();
  }

  setAccountBank(String value) {
    payInfoForm.accountBank = value;
    notifyListeners();
  }

  setAccountNumber(String value) {
    payInfoForm.accountNumber = value;
    notifyListeners();
  }

  setBankAddressCode(cityPicker.Result value) {
    payInfoForm.bankAddressCode = value.areaId ?? value.cityId ?? value.provinceId;
    payInfoForm.bankProvince = value.provinceName;
    payInfoForm.bankCity = value.cityName;
    payInfoForm.bankDistrict = value.areaName;

    notifyListeners();
  }

  setAccountName(String value) {
    payInfoForm.accountName = value;
    notifyListeners();
  }

  setPhone(String value) {
    payInfoForm.phone = value;
    notifyListeners();
  }

  setAlipayAccount(String value) {
    payInfoForm.alipayAccount = value;
    notifyListeners();
  }

  setEmial(String value) {
    payInfoForm.email = value.trim();
    notifyListeners();
  }

  setIdCardCopy(String value) {
    payInfoForm.idCardCopy = value;
    notifyListeners();
  }

  setIdCardNational(String value) {
    payInfoForm.idCardNational = value;
    notifyListeners();
  }

  setIdCardNumber(String value) {
    payInfoForm.idCardNumber = value;
    notifyListeners();
  }

  setIdCardStartTime(DateTime dateTime) {
    payInfoForm.idCardStartTime = flustars.DateUtil.formatDate(dateTime, format: 'yyyy-MM-dd');
    notifyListeners();
  }

  setIdCardEndTime(DateTime dateTime) {
    payInfoForm.idCardEndTime = flustars.DateUtil.formatDate(dateTime, format: 'yyyy-MM-dd');
    notifyListeners();
  }

  setStoreEntrancePic(String value) {
    payInfoForm.storeEntrancePic = value;
    notifyListeners();
  }

  setIndoorPic(String value) {
    payInfoForm.indoorPic = value;
    notifyListeners();
  }

  setBusinessLicense(String value) {
    payInfoForm.businessLicense = value;
    notifyListeners();
  }

  addOrUpdateMchInfo() async {
    if (!RegUtil.testNotEqEmpty(payInfoForm.bankCardPositive) || !RegUtil.testNotEqEmpty(payInfoForm.bankCardBack)) {
      toast('请上传银行卡正反面照片');
      return;
    }

    if (payInfoForm.accountType == null) {
      toast('请选择银行账户');
      return;
    }

    if (!RegUtil.testNotEqEmpty(payInfoForm.accountBank)) {
      toast('请填写开户银行');
      return;
    }

    if (!RegUtil.testNotEqEmpty(payInfoForm.bankName)) {
      toast('请填写开户银行全称');
      return;
    }

    if (!RegUtil.testNotEqEmpty(payInfoForm.accountNumber)) {
      toast('请填写银行卡号');
      return;
    }

    if (!RegUtil.testNotEqEmpty(payInfoForm.bankAddressCode)) {
      toast('请选择开户银行所在地');
      return;
    }

    if (!RegUtil.testProvincialCode(payInfoForm.bankAddressCode)) {
      toast('请输入正确的开户银行省市编码');
      return;
    }

    if (!RegUtil.testNotEqEmpty(payInfoForm.accountName)) {
      toast('请输入开户姓名');
      return;
    }

    if (!RegUtil.testNotEqEmpty(payInfoForm.phone)) {
      toast('请输入手机号码');
      return;
    }

    if (!RegUtil.testPhone(payInfoForm.phone)) {
      toast('请输入正确的手机号码');
      return;
    }

    if (!RegUtil.testNotEqEmpty(payInfoForm.alipayAccount)) {
      toast('请输入支付宝账号');
      return;
    }

    if (!RegUtil.testNotEqEmpty(payInfoForm.email)) {
      toast('请输入邮箱');
      return;
    }

    if (!RegUtil.testEmail(payInfoForm.email)) {
      toast('请输入正确的邮箱');
      return;
    }

    if (!RegUtil.testNotEqEmpty(payInfoForm.idCardCopy) || !RegUtil.testNotEqEmpty(payInfoForm.idCardNational)) {
      toast('请上传身份证正反面照片');
      return;
    }

    if (!RegUtil.testNotEqEmpty(payInfoForm.idCardNumber)) {
      toast('请输入身份证号码');
      return;
    }

    if (!flustars.RegexUtil.isIDCard(payInfoForm.idCardNumber)) {
      toast('请输入正确的身份证号码');
      return;
    }

    if (!RegUtil.testNotEqEmpty(payInfoForm.idCardStartTime)) {
      toast('请选择身份证有效日期');
      return;
    }

    if (!RegUtil.testNotEqEmpty(payInfoForm.storeEntrancePic) || !RegUtil.testNotEqEmpty(payInfoForm.indoorPic)) {
      toast('请上传门店门口店内环境照');
      return;
    }

    if (!RegUtil.testNotEqEmpty(payInfoForm.businessLicense)) {
      toast('请上传营业执照正面');
      return;
    }

    if (payInfoForm.accountType == BankAccountType.PUBLIC && !RegUtil.testNotEqEmpty(payInfoForm.openingPermit)) {
      toast('请上传用户许可证');
      return;
    }

    try {
      showLoadingToast();

      var response = await apiHelper.addOrUpdateMchInfo(payInfoForm);
      if (!response.successful()) {
        toast(response.msg);
        return;
      }

      payInfoForm = response.data.merchantPayInfo;
      wechatHistory = response.data.wechatHistory;
      alipayHistory = response.data.alipayHistory;
      notifyListeners();

      toast('添加成功');

      Navigator.popAndPushNamed(
        GlobalPageContext.instance.context,
        BindingReviewPage.pageName,
      );
    } catch (e) {
      toast(e.toString());
    } finally {
      hideLoadingToast();
    }
  }

  initData() async {
    try {
      var response = await api.queryMchInfo();
      if (!response.successful()) {
        toast(response.msg);
        return;
      }
      payInfoForm = response.data.merchantPayInfo ?? PayInfoForm.fromJson({});
      wechatHistory = response.data.wechatHistory;
      alipayHistory = response.data.alipayHistory;

      notifyListeners();
    } catch (e) {
      toast(e.toString());
    }
  }

  setAccountType(int value) {
    payInfoForm.accountType = value;
    notifyListeners();
  }

  setOpeningPermit(String value) {
    payInfoForm.openingPermit = value;
    notifyListeners();
  }
}
